package cn.trs.common.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @version 1.0
 * @author:TRS
 * @create:2023/2/24 15:22
 * @description:
 * @ClassName: TimeUtil
 * @company TRS
 */
public class TimeUtil {
    public static final String PATTERN_YYYYMMDD_HHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String PATTERN_YYYYMMDD = "yyyyMMdd";
    public static final String PATTERN_YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";

    /**
     * 获取当前时间
     *
     * @return 当前时间
     */
    public static Date now() {
        return Calendar.getInstance().getTime();
    }

    public static Integer nowSecond() {
        return Math.toIntExact(now().getTime() / 1000);
    }

    /**
     * 获取当前时间毫秒数
     * @return
     */
    public static Long nowMilliSecond() {
        return now().getTime();
    }

    /**
     * 获取当前月份
     *
     * @return 当前时间
     */
    public static Integer nowMonth() {
        return LocalDateTime.now().getMonthValue();
    }

    /**
     * 将当前时间转为秒
     *
     * @return 当前时间
     */
    public static int convertToSecond(Date date) {
        return Math.toIntExact(date.getTime() / 1000);
    }

    /**
     * 将秒转为Date
     *
     * @return 当前时间
     */
    public static Date convertToDate(long second) {
        return new Date(second * 1000);
    }
    
    /**
     * 获取当前月份第一日时间戳
     * @return
     */
    public static long nowMonthFirstDay() {
        LocalDateTime localDateTime = LocalDateTime.now().withDayOfMonth(1).withHour(0)
                .withMinute(0)
                .withSecond(0);
        // 获取中国标准时间的时区偏移（UTC+8）
        ZoneOffset chinaOffset = ZoneOffset.ofHours(8);
        return localDateTime.toEpochSecond(chinaOffset);
    }

    /**
     * 获取当日第0时间戳
     * @return
     */
    public static long todayStart() {
        LocalDateTime localDateTime = LocalDateTime.now().withHour(0)
                .withMinute(0)
                .withSecond(0);
        // 获取中国标准时间的时区偏移（UTC+8）
        ZoneOffset chinaOffset = ZoneOffset.ofHours(8);
        return localDateTime.toEpochSecond(chinaOffset);
    }

    /**
     * 获取当前小时第0分0秒时间戳
     * @return
     */
    public static long hourStart() {
        LocalDateTime localDateTime = LocalDateTime.now()
                .withMinute(0)
                .withSecond(0);
        // 获取中国标准时间的时区偏移（UTC+8）
        ZoneOffset chinaOffset = ZoneOffset.ofHours(8);
        return localDateTime.toEpochSecond(chinaOffset);
    }

    /**
     * 获取当前分钟第0秒时间戳
     * @return
     */
    public static long minuteStart() {
        LocalDateTime localDateTime = LocalDateTime.now()
                .withSecond(0);
        // 获取中国标准时间的时区偏移（UTC+8）
        ZoneOffset chinaOffset = ZoneOffset.ofHours(8);
        return localDateTime.toEpochSecond(chinaOffset);
    }

    /**
     * 获取半小时后时间
     *
     * @return 半小时后时间
     */
    public static Date halfAnHourLater() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, 30);
        return calendar.getTime();
    }

    /**
     * 获取当前时间几天后的时间
     * @return 当前时间几天前后的时间
     */
    public static Date xDaysBefore(int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }

    /**
     * 获取传入时间几天后的时间
     * @param date 传入的时间
     * @param days 几天后
     * @return 传入时间几天后的时间
     */
    public static Date xDaysBeforeToString(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }

    /**
     * 按照指定的格式，将当前时间格式化后返回
     *
     * @param pattern 指定的时间格式
     * @return 格式化后的当前时间
     */
    public static String formatNow(String pattern) {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 按照指定的格式，将当前时间格式化后返回
     *
     * @return 格式化后的当前时间
     */
    public static Integer formatNowToInteger() {
        return Integer.parseInt(LocalDateTime.now().format(DateTimeFormatter.ofPattern(PATTERN_YYYYMMDD)));
    }

    /**
     * Date类型转String
     * @param date
     * @return String
     */
    public static String formatDate(Date date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zone).toLocalDateTime();
        return formatter.format(localDateTime);
    }

    /**
     * Date类型转String
     * @param date
     * @return String
     */
    public static String formatDate(Date date, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zone).toLocalDateTime();
        return formatter.format(localDateTime);
    }

    /**
     * String类型转Date 自定义格式
     * @param date
     * @return String
     */
    public static Date dateToString(String date, String pattern) {
        if (pattern == null || pattern.isEmpty()) {
            pattern = PATTERN_YYYYMMDD_HHMMSS;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        Date resultDate = Date.from(zdt.toInstant());
        return resultDate;
    }

    /**
     * 毫秒级转化为秒级时间戳
     *
     * @param origin
     */
    public static void formatTimeForSecond(Long[]... origin) {
        for (Long[] times : origin) {
            for (int i = 0; times != null && i < times.length; i++) {
                times[i] /= 1000L;
            }
        }
    }
}

